<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
    <el-form ref="addFrom" label-width="120px" :model="addFrom" :rules="addRules">
      <el-form-item label="楼宇名称" prop="name">
        <el-input v-model.trim="addFrom.name" placeholder="输入楼宇名称" />
      </el-form-item>
      <el-form-item label="楼宇层数" prop="floors">
        <el-input v-model.trim="addFrom.floors" placeholder="请输入楼宇层数" />
      </el-form-item>
      <el-form-item label="在管面积" prop="area">
        <el-input v-model.trim="addFrom.area" placeholder="请输入在管面积">
          <template slot="append">m²</template>
        </el-input>
      </el-form-item>
      <el-form-item label="物业费" prop="propertyFeePrice">
        <el-input v-model.trim="addFrom.propertyFeePrice" placeholder="请输入物业费">
          <template slot="append">元/m²</template>
        </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row style="width:100%;" type="flex" justify="center">
        <el-button type="primary" size="mini" style="margin-right:50px" @click="btnCancel">取消</el-button>
        <el-button type="primary" size="mini" @click="btnOK">确认</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { addFloorAPI, getdateFloorAPI, updateFloorAPI } from '@/api/park'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      addFrom: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      addRules: {
        name: [{ required: true, message: '楼宇名称不能为空', trigger: 'change' }],
        floors: [{ required: true, message: '层数不能为空', trigger: 'change' }],
        area: [{ required: true, message: '在管面积不能为空', trigger: 'change' }],
        propertyFeePrice: [{ required: true, message: '物业费单价不能为空', trigger: 'change' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.addFrom.id ? '编辑楼宇' : '新增楼宇'
    }
  },
  methods: {
    btnCancel() {
      this.$emit('update:showDialog', false)
      this.$refs.addFrom.resetFields()
      this.addFrom = {
        id: null,
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      }
    },
    async btnOK() {
      await this.$refs.addFrom.validate()
      if (this.addFrom.id) {
        await updateFloorAPI(this.addFrom)
      } else {
        await addFloorAPI(this.addFrom)
        this.$message.success('新增楼宇成功')
      }
      this.btnCancel()
      this.$emit('updateDept')
    },
    async getFloor(id) {
      const res = await getdateFloorAPI(id)
      console.log(res)
      this.addFrom = {
        id: res.id,
        name: res.name,
        floors: res.floors,
        area: res.area,
        propertyFeePrice: res.propertyFeePrice
      }
    }
  }
}
</script>
<style>
</style>
